package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.BlogComment;

public class GenericBlogCommentDAO extends AutoKeys implements
		org.jetnuke.dao.BlogCommentDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericBlogCommentDAO.class);

	public BlogComment retrieveByPK(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("BlogCommentModel.selectByID"));
		p.setInt(1, id);

		ResultSet rs = p.executeQuery();
		BlogComment s = new BlogComment();
		if (rs.next()) {
			fillBlogCommentFromResultSet(s, rs);
		}

		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - end"); //$NON-NLS-1$
		}
		return s;
	}

	protected void fillBlogCommentFromResultSet(BlogComment s, ResultSet rs)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("fillBlogCommentFromResultSet(BlogComment, ResultSet) - start"); //$NON-NLS-1$
		}

		s.setAGENT(rs.getString("AGENT"));
		s.setAPPROVED(rs.getString("APPROVED"));
		s.setAUTHOR(rs.getString("AUTHOR"));
		s.setAUTHOR_EMAIL(rs.getString("AUTHOR_EMAIL"));
		s.setAUTHOR_IP(rs.getString("AUTHOR_IP"));
		s.setAUTHOR_URL(rs.getString("AUTHOR_URL"));
		s.setCONTENT(rs.getString("CONTENT"));
		s.setDATE(rs.getTimestamp("DATE"));
		s.setID(rs.getInt("ID"));
		s.setPARENT(rs.getInt("PARENT"));
		s.setPOST_ID(rs.getInt("POST_ID"));
		s.setTYPE(rs.getString("TYPE"));
		s.setUSER_ID(rs.getInt("USER_ID"));

		if (logger.isDebugEnabled()) {
			logger
					.debug("fillBlogCommentFromResultSet(BlogComment, ResultSet) - end"); //$NON-NLS-1$
		}
	}

	public void update(BlogComment bc) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("update(BlogComment) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("BlogCommentModel.update"));
		p.setString(1, bc.getAGENT());
		p.setString(2, bc.getAPPROVED());
		p.setString(3, bc.getAUTHOR());
		p.setString(4, bc.getAUTHOR_EMAIL());
		p.setString(5, bc.getAUTHOR_IP());
		p.setString(6, bc.getAUTHOR_URL());
		p.setString(7, bc.getCONTENT());
		p.setTimestamp(8, new Timestamp(bc.getDATE().getTime()));
		p.setInt(9, bc.getPARENT());
		p.setInt(10, bc.getPOST_ID());
		p.setString(11, bc.getTYPE());
		p.setInt(12, bc.getUSER_ID());
		p.setInt(13, bc.getID());
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("update(BlogComment) - end"); //$NON-NLS-1$
		}
	}

	public int addNew(BlogComment bp) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("addNew(BlogComment) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = this
				.getStatementForAutoKeys("BlogCommentModel.addNew");
		setAutoGeneratedKeysQuery(SystemGlobals
				.getSql("BlogCommentModel.lastGeneratedBlogCommentID"));
		initBlogComment(bp, p);

		int id = this.executeAutoKeysQuery(p);
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("addNew(BlogComment) - end"); //$NON-NLS-1$
		}
		return id;
	}

	protected void initBlogComment(BlogComment bc, PreparedStatement p)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("initBlogComment(BlogComment, PreparedStatement) - start"); //$NON-NLS-1$
		}

		p.setString(1, bc.getAGENT());
		p.setString(2, bc.getAPPROVED());
		p.setString(3, bc.getAUTHOR());
		p.setString(4, bc.getAUTHOR_EMAIL());
		p.setString(5, bc.getAUTHOR_IP());
		p.setString(6, bc.getAUTHOR_URL());
		p.setString(7, bc.getCONTENT());
		p.setTimestamp(8, new Timestamp(bc.getDATE().getTime()));
		p.setInt(9, bc.getPARENT());
		p.setInt(10, bc.getPOST_ID());
		p.setString(11, bc.getTYPE());
		p.setInt(12, bc.getUSER_ID());

		if (logger.isDebugEnabled()) {
			logger
					.debug("initBlogComment(BlogComment, PreparedStatement) - end"); //$NON-NLS-1$
		}
	}

	public List selectByPostID(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectByPostID(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("BlogCommentModel.selectByPOST_ID"));
		p.setInt(1, id);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectByPostID(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List selectAllByPostID(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectAllByPostID(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("BlogCommentModel.selectAllByPOST_ID"));
		p.setInt(1, id);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectAllByPostID(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	protected List processSelectAll(ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - start"); //$NON-NLS-1$
		}

		List list = new ArrayList();
		while (rs.next()) {
			BlogComment u = new BlogComment();
			fillBlogCommentFromResultSet(u, rs);
			list.add(u);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public void delete(BlogComment bc) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("delete(BlogComment) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("BlogCommentModel.delete"));
		p.setInt(1, bc.getID());
		p.executeUpdate();

		if (logger.isDebugEnabled()) {
			logger.debug("delete(BlogComment) - end"); //$NON-NLS-1$
		}
	}
}
